//
//  WMEClubViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/21.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEClubViewController.h"
#import "PrefixHeader.pch"
#import "WMECludModel.h"
#import "WMETextTableViewCell.h"
#import "WMEPhotoTableViewCell.h"
#import <AFNetworking.h>
#import <Masonry.h>
#import "WMEClubInformationViewController.h"
#import "WMEClubHeadView.h"
#import <MJRefresh.h>
#import "WMEImageViewController.h"

#pragma mark - club的根视图
@interface WMEClubViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;/**<主视图*/
@property (nonatomic, retain)NSMutableArray *dataArr;/**<数据数组*/
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSString *urlStr;/**<网络请求的字符*/
@property (nonatomic, retain)NSNumber *next_start;


@end

@implementation WMEClubViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_dic release];
    [_urlStr release];
    [_next_start release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //UIView.contentmode = UIViewContentModeScaleAspectFill;
    self.navigationItem.title = @"club";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self createView];
    [self data];
    [self loadingFunction];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    __block __typeof(&*self)bs = self;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bs.view.mas_left).with.offset(0);
        make.right.equalTo(bs.view.mas_right).with.offset(0);
        make.top.equalTo(bs.view.mas_top).with.offset(0);
        make.bottom.equalTo(bs.view.mas_bottom).with.offset(0);
    }];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = NO;
    [_tableView release];
}
#pragma mark -网络请求
- (void)data
{
    NSString *tempStr = @"http://www.duitang.com/napi/topic/list/by_tags/?__dtac=%257B%2522_r%2522%253A%2520%2522422174%2522%257D&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&limit=25&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&start=0&tags=精选";
    self.urlStr = [tempStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:tempStr]];
    
    //AFN网络请求
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.next_start = responseObject[@"data"][@"next_start"];
        self.dataArr = [WMECludModel modelHandlerWithArray:arr];

        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WMECludModel *m = _dataArr[indexPath.row];
    if (m.path.length != 0) {
        [tableView registerClass:[WMEPhotoTableViewCell class] forCellReuseIdentifier:@"cell1"];
        WMEPhotoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        photoCell.model = m;

        return photoCell;
    }else{
        [tableView registerClass:[WMETextTableViewCell class] forCellReuseIdentifier:@"cell"];
        WMETextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.model = m;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WMECludModel *model = _dataArr[indexPath.row];
    CGFloat h = [WMETextTableViewCell heightWithText:model.content];
//    NSLog(@"%lf", h);
    if (h > (15.51 * 2)) {
        h = 15.51 * 2 + 1;
    }
    if (model.path) {
        return 80 + 75.5 + h;
    }
    return 60.5 + h;
}
//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld", (long)indexPath.row);
    WMECludModel *model = _dataArr[indexPath.row];
    
    NSString *temp = @"http://203.80.144.213/napi/topic/detail/?include_fields=share_links&platform_version=4.4.4&device_platform=L36h&__dtac=%257B%2522_r%2522%253A%2520%2522187455%2522%257D&screen_width=1080&screen_height=1776&__domain=www.duitang.com&app_version=67&platform_name=Android&locale=zh&app_code=nayutas&topic_id=126492";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"id=126492" withString:[NSString stringWithFormat:@"id=%@", model.selfId.stringValue]];
    //AFN网络请求
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.dic = responseObject[@"data"];
        [_tableView reloadData];
        WMEClubInformationViewController *informationView = [[WMEClubInformationViewController alloc] init];
        informationView.interface = model.selfId.stringValue;
        informationView.headDic = _dic;
        informationView.commentCount = model.comment_count;
        [self.navigationController pushViewController:informationView animated:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

#pragma mark -加载
- (void)loadingFunction
{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)loadMoreData
{
    NSString *newUrlStr = [_urlStr stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%@", _next_start]];
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:newUrlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.next_start = responseObject[@"data"][@"next_start"];
        NSArray *tempArr = [WMECludModel modelHandlerWithArray:arr];
        [self.dataArr addObjectsFromArray:tempArr];
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
//        if (_next_start.integerValue > self.commentCount.integerValue) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }else{
//            [self.tableView.mj_footer endRefreshing];
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
